package com.yjd.comm.util;

import com.yjd.bi.report.model.BiDatabaseInfoModel;
import com.yjd.bi.report.model.BiTableInfoModel;
import com.yjd.bi.report.service.IBiDatabaseInfoService;
import com.yjd.bi.report.service.IBiTableInfoService;
import com.yjd.comm.base.enums.DbWREnums;
import com.yjd.comm.base.service.IBaseService;
import com.yjd.comm.enums.TableInfo;
import com.yjd.comm.rpm.RpmServiceKeyEnum;

public class TableTransferUtil {
	public static Long TRANSFER_BATCH=500l;

	
	//是否正在运行，false没有，true有数据表正在迁移
	private static boolean runing=false;
	
	public static void TransTableCheck(TableInfo info) throws Exception{
		if(info==null){
			FrameUtil.throwBaseException1("数据表不存在！");
		}
		if(check()){
			TransTable(info);
		}else{
			FrameUtil.throwBaseException1("有数据表正在迁移");
		}
	}
	
	public static void TransTableCheck(String schema,String table) throws Exception{
		if(check()){
			IBiTableInfoService biTableInfoService=(IBiTableInfoService)ServiceHolder.getService(IBiTableInfoService.class, "biTableInfoService", RpmServiceKeyEnum.RPM_BI_SERVICE_KEY_E);
			IBiDatabaseInfoService biDatabaseInfoService=(IBiDatabaseInfoService)ServiceHolder.getService(IBiDatabaseInfoService.class, "biDatabaseInfoService", RpmServiceKeyEnum.RPM_BI_SERVICE_KEY_E);
			BiDatabaseInfoModel fromDb=biDatabaseInfoService.getModelOne(FrameUtil.newHashMap("code",schema),DbWREnums.WRITE);
			BiDatabaseInfoModel toDb=biDatabaseInfoService.getModelOne(FrameUtil.newHashMap("code","bi_yijiedai_com"),DbWREnums.WRITE);
			if(fromDb==null){
				FrameUtil.throwBaseException1("数据表不存在！");
				return;
			}
			BiTableInfoModel fromTable=biTableInfoService.getModelOne(FrameUtil.newHashMap("table_name",table,"database_info_id",fromDb.getId()),DbWREnums.WRITE);
			if(fromTable==null){
				FrameUtil.throwBaseException1("数据表不存在！");
				return ;
			}
			BiTableInfoModel toTable=biTableInfoService.getModelOne(FrameUtil.newHashMap("table_name",table,"database_info_id",toDb.getId()),DbWREnums.WRITE);
			if(toTable==null){
				FrameUtil.throwBaseException1("数据表不存在！");
				return ;
			}
			TransTable(biTableInfoService,fromDb,toDb,fromTable,toTable);
		}else{
			FrameUtil.throwBaseException1("有数据表正在迁移");
		}
	}
	
	private static boolean check(){
		if(runing){
			return false;
		}
		runing=true;
		return true;
	}
	
	private static void TransTable(IBiTableInfoService biTableInfoService,BiDatabaseInfoModel fromDb,BiDatabaseInfoModel toDb,BiTableInfoModel fromTable,BiTableInfoModel toTable) throws Exception{
		try{
			Long total=biTableInfoService.transferFromOri(fromDb,toDb,fromTable);
			Long t=biTableInfoService.localCount(toDb,toTable);
			while(t<total){
				t=t+biTableInfoService.transferOne(fromDb,fromTable,toDb,toTable,t);
			}
			runing=false;
		}catch(Exception e){
			runing=false;
			throw(e);
		}
	}
	
	private static void TransTable(TableInfo info) throws Exception{
		if(info==null){
			return ;
		}
		try{
			IBaseService service=(IBaseService)ServiceHolder.getService(Class.forName(info.getClassname()), info.getServicename(), RpmServiceKeyEnum.RPM_BI_SERVICE_KEY_E);
			if(service==null){
				FrameUtil.throwBaseException1("找不到对应的service");
			}
			Long total=service.transferFromOri(info.getSchema(), info.getName());
			Long t=service.getModelListCount(FrameUtil.newHashMap(),DbWREnums.WRITE);
			while(t<total){
				t=t+service.transferBatch(info.getSchema(), t);
			}
			runing=false;
		}catch(Exception e){
			runing=false;
			throw(e);
		}
	}
}
